import aiohttp
from multiprocessing import Pool

class PageUtil():
	# default_page_size = 100
	DEFAULT_PAGE_SIZE = 100

	@classmethod
	def do_with_return(param_list:list, func, page_size:int):
		if is_param_valid(param_list)
			return
		if page_size < 1:
			page_size = DEFAULT_PAGE_SIZE;

        results = []


        total_count = len(params);
        page_count = (total_count - 1) / page_size + 1;

	    for i in range(page_count):
            start_index = i * page_size;
            end_index = (i + 1) * page_size > total_count ? total_count : (i + 1) * page_size;
            sub_params = params[start_index, end_index];
            # log.debug("before totalPageCount {}, currentPage {}, currentSize {}", new Object[]{pageCount, i + 1, subParams.size()});
            # Stopwatch stopwatch = Stopwatch.createStarted();
            piece_result = func(subParams);
            results.add(piece_result)
            # log.debug("after fetch size {}, cost {}", pieceResultList.size(), stopwatch.elapsed(TimeUnit.MILLISECONDS));
        return results


    @classmethod
    def do_without_return(param_list:list, func, page_size:int):
		if is_param_valid(param_list)
			return
		if page_size < 1:
			page_size = DEFAULT_PAGE_SIZE;

        results = []


        total_count = len(params);
        page_count = (total_count - 1) / page_size + 1;

	    for i in range(page_count):
            start_index = i * page_size;
            end_index = (i + 1) * page_size > total_count ? total_count : (i + 1) * page_size;
            sub_params = params[start_index, end_index];
            # log.debug("before totalPageCount {}, currentPage {}, currentSize {}", new Object[]{pageCount, i + 1, subParams.size()});
            # Stopwatch stopwatch = Stopwatch.createStarted();
            piece_result = func(subParams);
            results.add(piece_result)
            # log.debug("after fetch size {}, cost {}", pieceResultList.size(), stopwatch.elapsed(TimeUnit.MILLISECONDS));
        return results


    @classmethod
    async def do_with_async(param_list:list, func, page_size:int):
			return
		if page_size < 1:
			page_size = DEFAULT_PAGE_SIZE;

        results = []


        total_count = len(params);
        page_count = (total_count - 1) / page_size + 1;

	    for i in range(page_count):
            start_index = i * page_size;
            end_index = (i + 1) * page_size > total_count ? total_count : (i + 1) * page_size;
            sub_params = params[start_index, end_index];
            # log.debug("before totalPageCount {}, currentPage {}, currentSize {}", new Object[]{pageCount, i + 1, subParams.size()});
            # Stopwatch stopwatch = Stopwatch.createStarted();
            piece_result = await func(subParams);
            results.add(piece_result)
            # log.debug("after fetch size {}, cost {}", pieceResultList.size(), stopwatch.elapsed(TimeUnit.MILLISECONDS));
        return results



	def is_param_valid(param_list):
		if param_list is None:
			return False
		if len(param_list) == 0:
			return False
		return True
		